﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpBolt.BasicIntefaces;
using SharpBolt.Basics;
using IrrKlang;
using SharpBolt;

namespace PowerOne.SoundUtilites
{
    public class KlangSoundRender : ISoundRender
    {
        ISoundEngine engine;

        public KlangSoundRender()
        {
            engine = new IrrKlang.ISoundEngine();
        }


        public void FreeSoundSource(string name)
        {
            engine.RemoveSoundSource(name);
        }

        public SbSoundSource GetSoundSource(string name)
        {
            SbSoundSource source = new SbSoundSource();
            ISoundSource irrSource = engine.GetSoundSource(Engine.ResourcesPrefix + name);
            source.data = irrSource;
            source.length = irrSource.PlayLength;
            source.name = name;

            return source;
        }

        public SbSound GetEmmitter(SbSoundSource source)
        {
            ISound irrSound = engine.Play2D(source.data as ISoundSource, false, true, false);
            SbSound sound = new SbSound();
            sound.data = irrSound;
            sound.source = source;
            return sound;
        }

        public void FreeEmmitter(SbSound sound)
        {

        }

        public void Play(SbSound sound, bool loop = false)
        {

            ISound irrSound = (sound.data as ISound);


            ISoundSource source = sound.source.data as ISoundSource;
            source.DefaultVolume = irrSound.Volume;
            irrSound = engine.Play2D(source, loop, false, false);

            sound.data = irrSound;
        }

        public void Stop(SbSound sound)
        {
            (sound.data as ISound).Stop();
        }

        public void Pause(SbSound sound)
        {
            (sound.data as ISound).Paused = true;
        }

        public void Continue(SbSound sound)
        {
            (sound.data as ISound).Paused = false;
        }

        public void SetVolume(SbSound sound, double volume)
        {
            ISoundSource source = sound.source.data as ISoundSource;
            source.DefaultVolume = (float)volume;
            (sound.data as ISound).Volume = (float)volume;
        }

        public void SetGlobalVolume(double globalVolume)
        {
            engine.SoundVolume = (float)globalVolume;
        }


        public bool IsStopped(SbSound sound)
        {
           return (sound.data as ISound).Finished;
        }


       
    }
}
